/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package JKPanels;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import java.io.File;
import java.util.List;
import java.util.Arrays;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import jktest.JKMainPanel;
import jktest.JKMainGui;
import jktest.JKButton;

/**
 *
 * @author Royce Feng
 */
public class ExtendedSoftwarePanel extends JKMainPanel {
    
    private JScrollPane pane;
    private JTextArea textArea;
    
    //Panels for labels
    private JPanel mainLabelPanel;
    private JPanel softwareLabelz;
    private JPanel resultLabels;
    
    // Labels for software
    private JLabel avast;
    private JLabel imgburn;
    private JLabel adobe;
    private JLabel mozilla;
    private JLabel Zip;
    
    // Labels for software result
    private static JLabel avastR = new JLabel("");
    private static JLabel imgburnR = new JLabel("");
    private static JLabel adobeR = new JLabel("");
    private static JLabel mozillaR = new JLabel("");
    private static JLabel ZipR = new JLabel(""); 
    
    //Avast Test
    private String[] avastFiles = {"Aavm4h.dll","AavmGuih.dll","AavmRpch.dll","AhResMai.dll","ahResMes.dll","AhResNS.dll",
            "AhResOut.dll","ahResP2P.dll","AhResStd.dll","AhResWS.dll","AhRuiMai.dll","ahRuiMes.dll","AhRuiNS.dll","AhRuiOut.dll",
            "ahRuiP2P.dll","AhRuiStd.dll","AhRuiWS.dll","ashAvast.exe","ashBase.dll","ashBug.exe","ashCfgP.dll","ashCfgT.dll",
            "ashChest.dll","ashChest.exe","ashCnsnt.exe","ashDisp.exe","ashLogV.exe","ashMaiSv.exe","ashOutXt.dll","ashPopWz.exe",
            "ashQuick.exe","ashServ.exe","ashShA64.dll","ashShell.dll","ashSimp2.exe","ashSimpl.exe","ashSkPcc.exe","ashSkPck.exe",
            "ashSODBC.dll","ashSSqlt.dll","ashSXML.dll","ashTask.dll","ashUInt.dll","ashUpd.exe","ashWebSv.exe","ashWsFtr.dll",
            "aswAux.dll","aswChLic.exe","aswCmnB.dll","aswCmnOS.dll","aswCmnS.dll","aswEngin.dll","aswIdle.dll","aswInteg.dll",
            "aswMonDS.sys","aswMonVD.dll","aswRawFS.dll","aswRegSvr.exe","aswRes.dll","aswRunDll.exe","aswScan.dll","aswUpdSv.exe",
            "AVASTSS.scr","avCommEx.dll","AVSSHOOK.dll","DATA","DefTasks.xml","ENGLISH","images","sched.exe","Setup","VisthAux.exe",
            "VisthLic.exe","VisthUpd.exe","wdp-ash-updscript.vbs","XT1922.dll"};

    //Mozilla Test
    private String[] mozillaFiles = {"AccessibleMarshal.dll","application.ini","blocklist.xml","browserconfig.properties",
            "chrome","components","crashreporter-override.ini","crashreporter.exe","crashreporter.ini","defaults","dictionaries",
            "extensions","firefox.exe","freebl3.chk","freebl3.dll","greprefs","js3250.dll","LICENSE","modules","mozcrt19.dll",
            "nspr4.dll","nss3.dll","nssckbi.dll","nssdbm3.dll","nssutil3.dll","old-homepage-default.properties","override.ini",
            "platform.ini","plc4.dll","plds4.dll","plugins","README.txt","res","searchplugins","smime3.dll","softokn3.chk",
            "softokn3.dll","sqlite3.dll","ssl3.dll","updater.exe","updater.ini","xpcom.dll","xul.dll"};
    
    //Adobe Test
    private String[] adobeFiles = {"A3DUtility.exe","ACE.dll","AcroBroker.exe","Acrofx32.dll","AcroRd32.dll","AcroRd32.exe",
            "AcroRd32Info.exe","AcroRdIF.dll","AcroTextExtractor.exe","AdobeCollabSync.exe","AdobeLinguistic.dll","AdobeUpdater.dll",
            "AdobeXMP.dll","AGM.dll","AGMGPUOptIn.ini","ahclient.dll","AIR","AMT","atl.dll","authplay.dll","AXE8SharedExpat.dll",
            "AXSLE.dll","BIB.dll","BIBUtils.dll","Browser","ccme_base.dll","CoolType.dll","cryptocme2.dll","cryptocme2.sig",
            "Eula.exe","icucnv36.dll","icudt36.dll","IDTemplates","Javascripts","JP2KLib.dll","Legal","logsession.dll",
            "LogTransport2.dll","LogTransport2.exe","Onix32.dll","Optional","PDFPrevHndlr.dll","PDFPrevHndlrShim.exe",
            "PDFSigQFormalRep.pdf","pe.dll","plug_ins","plug_ins3d","pmd.cer","reader_sl.exe","rt3d.dll","RTC.der","SPPlugins",
            "sqlite.dll","Tracker","vdk150.dll","ViewerPS.dll"};
    
    //7-Zip Test
    private String[] zipFiles = {"7-zip.chm","7-zip.dll","7z.dll","7z.exe","7z.sfx","7zCon.sfx","7zFM.exe","7zG.exe",
    "7zip_pad.xml","copying.txt","descript.ion","History.txt","Lang","License.txt","readme.txt","Uninstall.exe"};
    
    //ImgBurn Test
    private String[] imgburnFiles = {"ImgBurn.exe","ImgBurnPreview.exe","Languages","License Agreement.rtf","ReadMe.txt",
        "Sounds","uninstall.exe"};

    // To keep up with stuff...
    private String[] keys = {"Avast","ImgBurn","Adobe Reader","Mozilla","7Zip"};
    private JLabel[] softwares = {avast,imgburn,adobe,mozilla,Zip};
    private JLabel[] results = {avastR,imgburnR,adobeR,mozillaR,ZipR};
    private String[] roots = {"\\Alwil Software\\Avast4","\\ImgBurn","\\Adobe\\Reader 9.0\\Reader","\\Mozilla Firefox","\\7-Zip"};
    private String[][] files = {avastFiles,imgburnFiles,adobeFiles,mozillaFiles,zipFiles};
    
    /**
     * 
     * Helper Function. This creates the two seperate columns (one 
     * for software names and one for the result).
     * 
     */
    private void loadLabels() {
        mainLabelPanel = new JPanel(new GridLayout(1,2));
        mainLabelPanel.setPreferredSize(new Dimension(200,80));
        softwareLabelz = new JPanel();
        softwareLabelz.setSize(250,80);
        resultLabels = new JPanel();
        resultLabels.setSize(250,80);
        softwareLabelz.setLayout(new BoxLayout(softwareLabelz,BoxLayout.Y_AXIS));
        resultLabels.setLayout(new BoxLayout(resultLabels,BoxLayout.Y_AXIS));
        for ( int i = 0; i < keys.length; i++ ) {
            softwares[i] = new JLabel(keys[i]);
            softwareLabelz.add(softwares[i]);
            resultLabels.add(results[i]);
        }
        mainLabelPanel.add(softwareLabelz);
        mainLabelPanel.add(resultLabels);
        this.add(mainLabelPanel);
    }
    
    /**
     * 
     * Main Constructor for ExtendedSoftwarePanel.
     * 
     * @param _parent The Parent Class (for communication)
     */
    public ExtendedSoftwarePanel ( JKMainGui _parent ) {
        super(_parent);
        JKButton button = new JKButton(this,"Start");
        textArea = new JTextArea(10,20);
        textArea.setEditable(false);
        pane = new JScrollPane(textArea);
        button.setPreferredSize(new Dimension(80,25));
        this.add(pane);
        loadLabels();
        this.add( button );
    }
    
    /**
     * 
     * Returns the name of the panel
     * 
     * @return The name of the panel
     */
    @Override
    public String getName() {
        return "Extended Software";
    }

    /**
     *
     * Runs all the tests as they correspond to the roots[] array. If the directory does not
     * exist, the test will skip that program.
     *
     */
    private void runTests() {
        int[] testResults = {0,0,0,0,0};
        for ( int i = 0; i < roots.length; i++ ) {
            String root = System.getenv("PROGRAMFILES") + roots[i];
            File f = new File(root);
            if ( f.exists() ) {
                String[] children = f.list();
                List l = Arrays.asList(children);
                for ( int j = 0; j < files[i].length; j++ ) {
                    textArea.append(files[i][j]);
                    if (l.contains(files[i][j])) {
                        testResults[i]++;
                        textArea.append("[Found]\n");
                    } else
                        textArea.append("[Missing]\n");
                }
                results[i].setText(testResults[i]+"/"+files[i].length);
            } else {
                results[i].setText("0/"+files[i].length);
            }
        }
    }

    /**
     * 
     * Handles button clicks. Pass/fail gets sent to parent while the test is run
     * when the test button is clicked.
     * 
     * @param source Button source (usually the button text)
     */
    public void ButtonClicked( String source ) {
        runTests();
    }
    
}
